<?php
/*
Plugin Name: HidePost
Plugin URI: http://nguyenthanhcong.com/hidepost-plugin-for-wordpress/
Description: Protect a part of your post by using [hidepost] and [/hidepost]  between the protected content.
Version: 2.3
Author: Fu4ny
Author URI: http://nguyenthanhcong.com
*/
include("option_init.php");

function replace_part(){
global $content_text, $link_text;
$content_text = stripslashes($content_text);
$link_text = stripslashes($link_text);
$content_text = str_replace('%login%','<a href='.get_bloginfo('url').'/wp-login.php'.'>Login</a>',$content_text);
$content_text = str_replace('%register%','<a href='.get_bloginfo('url').'/wp-login.php?action=register'.'>Register</a>',$content_text);
$link_text = str_replace('%login%','<a href='.get_bloginfo('url').'/wp-login.php'.'>Login</a>',$link_text);
$link_text = str_replace('%register%','<a href='.get_bloginfo('url').'/wp-login.php?action=register'.'>Register</a>',$link_text);
}


function replace_hide($content) {
	global $current_user, $user_ID, $content_text,$user_level, $m_id;
	$m_id++;
	preg_match_all('#\[hidepost(.*?)\](.*?)\[/hidepost\]#sie', $content, $matches);
	$level_tag = $matches[1][$m_id];
	$max_level = 0;
	if ($level_tag[0] == '=') {
		$max_level = $level_tag[1];
	}
	$allow_bot = get_option('allow_bot');
	if ($allow_bot == 1) {
		$useragent = $_SERVER['HTTP_USER_AGENT'];
		if (stristr('google msnbot ia_archiver lycos jeeves scooter fast-webcrawler slurp@inktomi turnitinbot technorati yahoo findexa findlinks gaisbo zyborg surveybot bloglines blogsearch pubsub syndic8 userland  gigabot become.com', $useragent) !== false) {
				return $matches[2][$m_id];
			}
	}
	get_currentuserinfo();
	if ($user_ID == ''){//If not logged in
		return $content_text;
	}
	if ($user_level < $max_level) {
		$show = '<b>You must be a(n) <font size=3 color=BLUE>';
		switch ($max_level):
			case 0: $show .= 'Subscriber';
					break;
			case 1: $show .= 'Contributor';
					break;
			case 2: $show .= 'Author';
					break;
			case 3:
			case 4:
			case 5:
			case 6:
			case 7: $show .= 'Editor';
					break;
			case 8:
			case 9: $show .= 'Administrator';
					break;
		endswitch;
		$show .= ' </font>to view this content</b>';
		return $show;
	}
	return $matches[2][$m_id];
}
function replace_link($content) {
	global $current_user, $user_ID, $link_text, $m_id;
	$m_id++;
	preg_match_all('#\<a(.*?)\>(.*?)\</a\>#sie', $content, $matches);
	get_currentuserinfo();
	if ($user_ID == ''){//If not logged in
		return $link_text;
	} else {return $matches[0][$m_id];}
}

function filter_post($content) {
    global $m_id, $hide_link, $hide_content;
	replace_part();
	//Protect the link
	if ($hide_link == 1) {
	$m_id = -1;//Magic ^.^
	$content = preg_replace('#\<a(.*?)\>(.*?)\</a\>#sie','replace_link($content)',$content); 
	}
	//Protect content
	if ($hide_content == 1) {
	$m_id = -1;
	$content = preg_replace('#\[hidepost(.*?)\](.*?)\[/hidepost\]#sie','replace_hide($content)',$content);
	}
	return $content;
}

//Add the Option Page
function hidepost_options()	{
        if (function_exists('add_options_page')) {
            add_options_page('HidePost Options','HidePost', 6, 'hidepost/options.php');
        }
    }
add_filter('the_content', 'filter_post');
add_action('admin_menu', 'hidepost_options');
?>